package finman.database;

/**
 * Represents a bank account. Has a number, a current balance and
 * a currency in which account has been opened.
 */
public class Schet {
	private String number;
	private Valuta valuta;
	private double balance;

    /**
     * Creates account and initialize it by required parameters:
     * number, currency, init balance.
     */
	public Schet(String number, Valuta valuta, double balance) {
		this.number = number;
		this.valuta = valuta;
		this.balance = balance;
	}

    /**
     * Compares 2 account numbers. Required for class Bank.
     */
	public int compareNumberTo(String anotherNumber) {
		// comparing 2 account numbers
		return number.compareTo(anotherNumber);
	}
	/**
     * Generate and returns next account number following after someNumber.
	 * @param someNumber Pass <b>null</b> to retrieve the first
	 * possible value of account number.
	 */
	public static String getNextAccountNumber(String someNumber) {
		final String firstAccountNumber = "26030320036200";
		if(someNumber == null) return firstAccountNumber;
		Long number = Long.parseLong(someNumber);
		number = number + 1;
		return number.toString();
	}

    /**
     * Returns a number of this account.
     */
	public String getNumber() {
		return number;
	}

    /**
     * Returns a current balance of this account.
     */
    public double getBalance() {
        return balance;
    }

    /**
     * Returns a currency in which account has been opened.
     */
    public Valuta getValuta() {
        return valuta;
    }


    @Override
    public int hashCode() {
        int hashCode = 0;
        if (number != null) {
            hashCode += number.hashCode();
        }
        if (valuta != null) {
            hashCode += valuta.hashCode();
        }
        return hashCode + (int)(17*balance);
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Schet)) {
            return false;
        }
        Schet givenAccount = (Schet) obj;
        if (balance != givenAccount.balance) return false;

        // check if 'obj' hold an non-null field that is null in this one!
        if (
                ((number == null)&&(givenAccount.number != null)) ||
                ((valuta == null)&&(givenAccount.valuta != null))
           )
        {
            return false;
        }
        // whether all field are equal ?
        if ((number != null) && (!number.equals(givenAccount.number))) {
            return false;
        }
        if ((valuta != null) && (! valuta.equals(givenAccount.valuta))) {
            return false;
        }
        return true;

    }

    /**
     * Returns string that contains account number,
     * balance snd string representation of currency in which account was opened.
     */
    @Override
    public String toString() {
        return String.format("\"%s\" %.2f in '%s'", number, balance, valuta);
    }
}